package view;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.LinkedList;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import adamsLibrary.JComboBoxAuto;
import adamsLibrary.JComboBoxAutoLinkedList;

import control.OrderControl;

import java.awt.Color;

public class RegisterDeliveryPanel extends JPanel {
	private OrderControl oc;
	private JComboBoxAuto barcodeComboBox;
	private JPanel op;
	private JComboBox customerComboBox;
	public RegisterDeliveryPanel(OrderControl oc) {
		setBackground(Color.BLACK);
		
		this.oc=oc;
		op = this;
		JButton registerBtn = new JButton("Register");
		registerBtn.addActionListener(new registerPallets());
		
		barcodeComboBox = new JComboBoxAuto(new GetBarcodeList());
		
		
		JLabel lblCustomer = new JLabel("Barcode");
		lblCustomer.setForeground(Color.GREEN);
		
		customerComboBox = new JComboBoxAuto(new GetCustomerList());
		
		JLabel lblCustomer_1 = new JLabel("Customer");
		lblCustomer_1.setForeground(Color.GREEN);
		
		
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblCustomer)
						.addComponent(lblCustomer_1))
					.addGap(38)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(registerBtn)
						.addComponent(barcodeComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(customerComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(194, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(barcodeComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblCustomer))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(40)
							.addComponent(registerBtn))
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblCustomer_1)
							.addComponent(customerComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(191, Short.MAX_VALUE))
		);
		setLayout(groupLayout);
	}
	
	private class GetBarcodeList implements JComboBoxAutoLinkedList{	
		
		@Override
		public LinkedList<String> getList(String string) {
			return oc.getBarcodes(string);
		}		
	}
	
	private class GetCustomerList implements JComboBoxAutoLinkedList{	
		
		@Override
		public LinkedList<String> getList(String string) {
			return oc.getCustomers(string);
		}		
	}
	
	private class registerPallets implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String barcode = barcodeComboBox.getEditor().getItem().toString();
			String customer = customerComboBox.getEditor().getItem().toString();
			oc.registerNewDelivery(barcode,customer);
		}	
	}
}
